
#include "mainwindow.h"
#include "mainwindow_p.h"
#include "login.h"
#include "animationstackedwidget.h"
#include "registerpage.h"
#include "mainwindowtitle.h"
#include "moveframelesswidget.h"
#include "systemtray.h"
#include <QVBoxLayout>
#include <QPainter>
#include <QSvgRenderer>
#include <QFile>
#include <QDebug>

MainWindowPrivate::MainWindowPrivate(MainWindow *q)
    : q_ptr(q)
{
    initUi();
}

MainWindowPrivate::~MainWindowPrivate()
{

}

void MainWindowPrivate::initUi()
{
    QWidget *widget = new QWidget(q_ptr);
    widget->setObjectName("back_widget");

    stack_widget_ = new AnimationStackedWidget(widget);
    login_widget_ = new Login(widget);
    register_widget_ = new RegisterPage(widget);
    title_widget_ = new MainWindowTitle(widget);

    QHBoxLayout *layout = new QHBoxLayout(q_ptr);
    layout->addWidget(widget);
    layout->setMargin(0);
    auto *vLayout = new QVBoxLayout(widget);
    vLayout->addWidget(title_widget_);
    vLayout->addWidget(stack_widget_);
    vLayout->setMargin(0);
    vLayout->setSpacing(0);

    stack_widget_->addWidget(login_widget_);
    stack_widget_->addWidget(register_widget_);
    stack_widget_->setCurrentIndex(1);
    stack_widget_->setCurrentIndex(0);
    QObject::connect(login_widget_, &Login::sig_register, [this](){
        stack_widget_->setStartValue(0);
        stack_widget_->setStopValue(180);
        stack_widget_->animation(1);
    });
    QObject::connect(register_widget_, &RegisterPage::sig_login, [this](){
        stack_widget_->setStartValue(0);
        stack_widget_->setStopValue(180);
        stack_widget_->animation(0);
    });
    QObject::connect(login_widget_, &Login::sig_login, [this](){
        q_ptr->accept();
    });
    QObject::connect(title_widget_, &MainWindowTitle::sig_closeBtnClicked, [this](){
        q_ptr->reject();
    });
}

MainWindow::MainWindow(QWidget *parent)
    : MoveFramelessDialog(parent)
    , d_ptr(new MainWindowPrivate(this))
{
    setFixedSize(300, 300);
    // 加载 SVG 文件
    QSvgRenderer svgRenderer(QStringLiteral(":/resources/svg/turbo.svg"));

    // 创建一个 QPixmap，大小与 SVG 默认大小匹配
    QPixmap pixmap(25, 25);
    pixmap.fill(Qt::transparent);  // 设置背景透明
    // 使用 QPainter 将 SVG 渲染到 QPixmap
    QPainter painter(&pixmap);
    svgRenderer.render(&painter);
    setWindowIcon(QIcon(pixmap));

    system_tray_ = new SystemTray(this);
}

MainWindow::~MainWindow()
{
    if (d_ptr)
    {
        delete d_ptr;
        d_ptr = nullptr;
    }
}
